package com.rwaste.worker.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.rwaste.commonutils.R;
import com.rwaste.worker.entity.DetailInfo;
import com.rwaste.worker.entity.vo.DetailListQuery;
import com.rwaste.worker.service.DetailInfoService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ly
 * @since 2021-09-25
 */
@RestController
@RequestMapping("/worker/detailInfo")
public class DetailInfoController {
    @Autowired
    private DetailInfoService detailInfoService;

    //多条件查询  详细信息
    @ApiOperation(value = "查询详细信息")
    @PostMapping("/findALlDetailInfo")
    public R findALlDetailInfo(@RequestBody DetailListQuery detailListQuery){
        Integer page = detailListQuery.getPage();
        Integer limit = detailListQuery.getLimit();
        System.out.println(page+limit);

        String baseid = detailListQuery.getBaseid();
        Date creationTime = detailListQuery.getCreationTime();
        String idCard = detailListQuery.getIdCard();
        String zfbId = detailListQuery.getZfbId();

        QueryWrapper<DetailInfo> wrapper = new QueryWrapper<>();
        wrapper.eq("is_deleted", 0);
        if(!StringUtils.isEmpty(baseid)){
            wrapper.eq("baseid", baseid);
        }
        if(!StringUtils.isEmpty(idCard)){
            wrapper.eq("id_card", idCard);
        }
        if(!StringUtils.isEmpty(zfbId)){
            wrapper.eq("zfb_id", zfbId);
        }
        if(creationTime!=null){
            wrapper.eq("creation_time", creationTime);
        }
        Page<DetailInfo> detailInfoPage = new Page<>(page,limit);

        detailInfoService.page(detailInfoPage, wrapper);

        long total = detailInfoPage.getTotal();
        List<DetailInfo> records = detailInfoPage.getRecords();
        return R.ok().data("total",total).data("list",records);

    }

    @ApiOperation(value = "根据id查询信息")
    @GetMapping("/findDetailInfoById/{id}")
    public R findDetailInfoById(@PathVariable String id) {
        QueryWrapper<DetailInfo> wrapper = new QueryWrapper<>();
        wrapper.eq("baseid", id);
        DetailInfo byId = detailInfoService.getOne(wrapper);
        if(byId!=null){
            return R.ok().data("detailInfo",byId);
        }
        return R.error();
    }
}

